package com.zyw.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.commonUtil.util.ResultCode;
import com.zyw.dao.employeeManageDao;
import com.zyw.entity.employeeManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@RestController

public class employeeManageController {

    @Autowired
    private employeeManageDao employeeManageDao;


    /**
     * 查询员工列表返回类型ArrayList
     *
     * @return
     */
    @RequestMapping("/list")
    public ResultCode queryAll() {
        List<employeeManage> list = employeeManageDao.selectList(null);
        list.forEach(System.out::println);
        return ResultCode.success(list);

    }


    @RequestMapping("/eques")
    public ResultCode query(String userName){
        ArrayList<employeeManage> arrayList= new ArrayList<>();
        ArrayList<Object> arrayList1 = new ArrayList<>();
        ArrayList<Object> slist = new ArrayList<>();
        LambdaQueryWrapper<employeeManage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(employeeManage::getUserName,userName);
        List<employeeManage> list = employeeManageDao.selectList(wrapper);
        for (employeeManage employeeManage : list) {
            employeeManage em = new employeeManage();
            em.setIdCard(employeeManage.getIdCard());
            em.setId(employeeManage.getId());
            em.setMaritalStatus(employeeManage.getMaritalStatus());
            em.setUserName(employeeManage.getUserName());
            em.setUserAvatar(employeeManage.getUserAvatar());
            arrayList.add(em);
        }
        arrayList1.add(arrayList);
        slist.add(arrayList1);
        return ResultCode.success(slist);
    }


    @RequestMapping("/like")
    public ResultCode queryAll(String userName,Integer idCard) {
        //展示全部列表
        if(userName!=null){
            QueryWrapper<employeeManage> wrapper = new QueryWrapper<>();
            wrapper.like("user_name",userName);
            List<employeeManage> list = employeeManageDao.selectList(wrapper);
            employeeManage em = new employeeManage();
            for (employeeManage employeeManage : list) {
                em.setIdCard(employeeManage.getIdCard());
            }
            return ResultCode.success(em);
        }else if(idCard!=null){
            QueryWrapper<employeeManage> wrapper = new QueryWrapper<>();
            wrapper.like("id_card",idCard);
            List<employeeManage> list = employeeManageDao.selectList(wrapper);
            return ResultCode.success(list);
        }else {
            System.out.println("============无查询条件显示所有信息====================");
        List<employeeManage> list = employeeManageDao.selectList(null);
            return ResultCode.success(list);
        }

    }

    @RequestMapping("/insert")
    public R insert(employeeManage employeeManage){
//        默认id生成方式为雪花算法
        int i = employeeManageDao.insert(employeeManage);
        if(i>0){
            return R.ok("success");
        }else {
         return R.failed("err");
        }
    }

}
